package com.ruoyi.kenuo.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.common.utils.RsaUtilClient;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.kenuo.domain.KernelMeasureRecord;
import com.ruoyi.kenuo.service.IKernelMeasureRecordService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 测量记录Controller
 *
 * @author polo tang
 * @date 2023-09-08
 */
@RestController
@RequestMapping("/kenuo/record")
public class KernelMeasureRecordController extends BaseController
{
    @Autowired
    private IKernelMeasureRecordService kernelMeasureRecordService;

    /**
     * 查询测量记录列表
     */
    //@PreAuthorize("@ss.hasPermi('kenuo:record:list')")
    @GetMapping("/list")
    public TableDataInfo list(KernelMeasureRecord kernelMeasureRecord) throws Exception {

        String measureUser = kernelMeasureRecord.getMeasureUser();
        //解密
        if(StringUtils.isNotEmpty(measureUser)){
            measureUser = RsaUtilClient.decryptByPrivateKeyTwo("",measureUser);
            kernelMeasureRecord.setMeasureUser(measureUser);
        }
        String measureUserName = kernelMeasureRecord.getMeasureUserName();
        if(StringUtils.isNotEmpty(measureUserName)){
            measureUserName = RsaUtilClient.decryptByPrivateKeyTwo("",measureUserName);
            kernelMeasureRecord.setMeasureUserName(measureUserName);
        }
        //医生的账号解密
        String doctorUser = kernelMeasureRecord.getDoctorUser();
        if(StringUtils.isNotEmpty(doctorUser)){
            doctorUser = RsaUtilClient.decryptByPrivateKeyTwo("",doctorUser);
            kernelMeasureRecord.setDoctorUser(doctorUser);
        }
        //医生的用户名解密
        String doctorUserName = kernelMeasureRecord.getDoctorUserName();
        if(StringUtils.isNotEmpty(doctorUserName)){
            doctorUserName = RsaUtilClient.decryptByPrivateKeyTwo("",doctorUserName);
            kernelMeasureRecord.setDoctorUserName(doctorUserName);
        }
        startPage();
        List<KernelMeasureRecord> list = kernelMeasureRecordService.selectKernelMeasureRecordList(kernelMeasureRecord);
        return getDataTable(list);
    }
    /**
     * 查询测量记录列表
     */
    //@PreAuthorize("@ss.hasPermi('kenuo:record:list')")
    @GetMapping("/listOne")
    public TableDataInfo listOne(KernelMeasureRecord kernelMeasureRecord) throws Exception {
        startPage();
        List<KernelMeasureRecord> list = kernelMeasureRecordService.selectKernelMeasureRecordList(kernelMeasureRecord);
        return getDataTable(list);
    }
    /**
     * 查询测量记录列表为单独接口
     */
    //@PreAuthorize("@ss.hasPermi('kenuo:record:list')")
    @GetMapping("/getDuList")
    public TableDataInfo getDuList(KernelMeasureRecord kernelMeasureRecord)
    {
        startPage();
        List<KernelMeasureRecord> list = kernelMeasureRecordService.selectKernelMeasureRecordList(kernelMeasureRecord);
        return getDataTable(list);
    }
    /**
     * 查询医生端异常提醒测量记录列表
     *  7天以内的
     *
     */
    @GetMapping("/getAbnormalList")
    public TableDataInfo getAbnormalList(KernelMeasureRecord kernelMeasureRecord)
    {
        startPage();
        List<KernelMeasureRecord> list = kernelMeasureRecordService.getAbnormalList(kernelMeasureRecord);
        return getDataTable(list);
    }

    /**
     * 查询医生端的异常提醒  （医生端）
     *  7天以内的
     *
     */
    @PostMapping("/getAbList")
    public TableDataInfo getAbList(@RequestBody KernelMeasureRecord kernelMeasureRecord)
    {
        String doctorUser = kernelMeasureRecord.getDoctorUser();
        if(StringUtils.isEmpty(doctorUser)){
            TableDataInfo rspData = new TableDataInfo();
            rspData.setCode(HttpStatus.ERROR);
            rspData.setMsg("The paramete 'doctorUser' is empty ");
            return rspData;
        }
        startPage();
        List<KernelMeasureRecord> list = kernelMeasureRecordService.getAbList(kernelMeasureRecord);
        return getDataTable(list);
    }
    /**
     * 查询患者端的异常提醒  （患者端）
     *  7天以内的
     *
     */
    @GetMapping("/getClientAbList")
    public TableDataInfo getClientAbList(KernelMeasureRecord kernelMeasureRecord) throws Exception {
        String measureUser = kernelMeasureRecord.getMeasureUser();
        if(StringUtils.isNotEmpty(measureUser)){
            measureUser = RsaUtilClient.decryptByPrivateKeyTwo("",measureUser);
            kernelMeasureRecord.setMeasureUser(measureUser);
        }
        String doctorUser = kernelMeasureRecord.getDoctorUser();
        if(StringUtils.isNotEmpty(doctorUser)){
            doctorUser = RsaUtilClient.decryptByPrivateKeyTwo("",doctorUser);
            kernelMeasureRecord.setDoctorUser(doctorUser);
        }
        startPage();
        List<KernelMeasureRecord> list = kernelMeasureRecordService.getClientAbList(kernelMeasureRecord);
        return getDataTable(list);
    }


    /**
     * 客户端查询最近测量的数据，包含左右眼
     */
    @GetMapping("/getRecentMeasure")
    public AjaxResult getRecentMeasure(KernelMeasureRecord kernelMeasureRecord) throws Exception {
        String measureUser = kernelMeasureRecord.getMeasureUser();
        if(StringUtils.isNotEmpty(measureUser)){
            measureUser = RsaUtilClient.decryptByPrivateKeyTwo("",measureUser);
            kernelMeasureRecord.setMeasureUser(measureUser);
        }
        AjaxResult ar = kernelMeasureRecordService.getRecentMeasure(kernelMeasureRecord);
        return ar;
    }
    /**
     * 导出测量记录列表
     */
    @PreAuthorize("@ss.hasPermi('kenuo:record:export')")
    @Log(title = "测量记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, KernelMeasureRecord kernelMeasureRecord)
    {
        List<KernelMeasureRecord> list = kernelMeasureRecordService.selectKernelMeasureRecordList(kernelMeasureRecord);
        ExcelUtil<KernelMeasureRecord> util = new ExcelUtil<KernelMeasureRecord>(KernelMeasureRecord.class);
        util.exportExcel(response, list, "测量记录数据");
    }

    /**
     * 获取测量记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('kenuo:record:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(kernelMeasureRecordService.selectKernelMeasureRecordById(id));
    }

    /**
     * 新增测量记录
     */
    //@PreAuthorize("@ss.hasPermi('kenuo:record:add')")
    @Log(title = "测量记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody KernelMeasureRecord kernelMeasureRecord)
    {
        return toAjax(kernelMeasureRecordService.insertKernelMeasureRecord(kernelMeasureRecord));
    }

    /**
     * 修改测量记录
     */
    @PreAuthorize("@ss.hasPermi('kenuo:record:edit')")
    @Log(title = "测量记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody KernelMeasureRecord kernelMeasureRecord)
    {
        return toAjax(kernelMeasureRecordService.updateKernelMeasureRecord(kernelMeasureRecord));
    }

    /**
     * 删除测量记录
     */
    @PreAuthorize("@ss.hasPermi('kenuo:record:remove')")
    @Log(title = "测量记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(kernelMeasureRecordService.deleteKernelMeasureRecordByIds(ids));
    }
    /**
     * 天周月 测量统计
     */
    @PostMapping("/getStatisticsList")
    public AjaxResult getStatisticsList(@RequestBody KernelMeasureRecord kernelMeasureRecord) throws Exception {

        String measureUser = kernelMeasureRecord.getMeasureUser();
        if(StringUtils.isNotEmpty(measureUser)){
            measureUser = RsaUtilClient.decryptByPrivateKeyTwo("",measureUser);
            kernelMeasureRecord.setMeasureUser(measureUser);
        }
        AjaxResult ar = kernelMeasureRecordService.getStatisticsList(kernelMeasureRecord);
        return ar;
    }
}
